<template>
  <div>
    <div class="top-panel__box">
      <el-form :model="roleForm" ref="roleForm" inline size="small" onsubmit="return false">
        <el-form-item>
          <el-button type="primary" @click="addRole">
            <fa-icon class="fa-plus" />新增职务</el-button>
        </el-form-item>
        <el-form-item>
          <el-input placeholder="请输入职务名称" v-model="roleForm.roleName" maxlength="30" clearable>
            <el-button slot="append" type="primary" icon="el-icon-search" @click="getList(1)"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item>
          <tooltip :tooltipOption="TOOLTIP_OPTION" />
        </el-form-item>
      </el-form>
    </div>
    <el-row class="table-top">
      <el-table :data="dataList" v-loading="tableLoading" tooltip-effect="light" class="tc-table" size="small">
        <el-table-column type="index" width="55" fixed="left" label="序号"></el-table-column>
        <template v-for="(column, index) in HEAD_LIST">
          <el-table-column :prop="column.prop" :label="column.label" :key="index" :width="column.width" :show-overflow-tooltip="true" />
        </template>
        <el-table-column prop="operation" class-name="oper-col" label="操作" :width="540">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" plain @click="editRow(scope.row)">编辑职务</el-button>
            <el-button size="mini" type="primary" plain @click="setRow(scope.row)">设置权限</el-button>
            <delete-button :role-data="scope.row" title="删除" :icon="false" type="danger" size="mini" @uppdataSuccess="uppdataRole" />
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row class="pagination-top">
      <pagination :pageNum.sync="roleForm.pageNum" :curSizes.sync="roleForm.pageSize" :total="total" @getList="getList" />
    </el-row>
    <!--新增职务-->
    <el-dialog :title="editData.roleId ? '编辑职务' : '新增职务'" :visible.sync="dialog.roleDialog" width="24%" :close-on-click-modal="false">
      <add-or-edit-role v-if="dialog.roleDialog" :editData="editData" @uppdataSuccess="updataData" @close="dialog.roleDialog = false"/>
    </el-dialog>
    <!--添加人员-->

    <!--设置权限-->
    <el-dialog title="设置权限" :visible.sync="dialog.setDialog" width="50%" :close-on-click-modal="false">
      <set-permission v-if="dialog.setDialog" :roleData="roleData" @uppdataSuccess="updataData" @updateList="getList(roleForm.pageNum)" />
    </el-dialog>
  </div>
</template>

<script>
import { TOOLTIP_OPTION, HEAD_LIST } from './js/contant.js'
import api from './api'
import { CONSTANT_OBJ } from '@/utils/constant'
// import { copyText } from '@/utils/util'
import { AddOrEditRole, SetPermission, StopRoleButton, OpenRoleButton, CopyButton, DeleteButton } from './components'
export default {
  components: { AddOrEditRole, SetPermission, StopRoleButton, OpenRoleButton, CopyButton, DeleteButton },
  data () {
    return {
      TOOLTIP_OPTION,
      HEAD_LIST,
      tableLoading: false,
      dialog: {
        roleDialog: false,
        userDialog: false,
        setDialog: false
      },
      editData: {},
      roleId: '',
      roleData: {},
      roleForm: {
        roleName: '',
        pageNum: 1,
        pageSize: 10
      },
      dataList: [],
      total: 0
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    // 查询列表
    getList (val) {
      this.roleForm.pageNum = val || this.roleForm.pageNum
      this.tableLoading = true
      api.postQueryRoleData(this.roleForm).then(res => {
        const { responseCode, data } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.dataList = data.list || []
          this.total = parseInt(data.total) || 0
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    /**
     * @description 复制
     */
    copyRow (row) {
      this.editData = { ...row, isCopy: true }
      this.dialog.roleDialog = true
    },
    /**
     * @description 添加人员
     */
    addUser (row) {
      this.roleId = row.roleId
      this.dialog.userDialog = true
    },
    /**
     * @description 新增
     */
    addRole () {
      this.editData = {}
      this.dialog.roleDialog = true
    },
    /**
     * @description 编辑
     */
    editRow (row) {
      this.editData = { ...row }
      this.dialog.roleDialog = true
    },
    // 设置权限
    setRow (row) {
      this.roleData = { ...row }
      this.dialog.setDialog = true
    },
    // 更新数据
    uppdataRole () {
      this.getList(1)
    },
    updataData (data) {
      this.dialog[`${data}Dialog`] = false
      this.getList(1)
    }
  }
}
</script>
